﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AioNightly.Model;
using MySql.Data.MySqlClient;

namespace AioNightly.Dao
{
    public class UserDAO
    {
        private void Read2List(MySqlDataReader reader, List<User> list)
        {
            while (reader.Read())
            {
                User it = new User();
                it.Id = reader.GetInt32(0);
                it.Type = reader.GetInt32(1);
                it.Name = reader.GetString(2);
                it.Passwd = reader.GetString(3);
                it.State = reader.GetInt32(4);

                list.Add(it);
            }
        }

        public bool ExistUser(string name)
        {
            DBConnect db = new DBConnect();
            db.OpenConnection();

            List<User> list = new List<User>();
            string sql = string.Format("select * from user where Name='{0}'", name);
            MySqlCommand cmd = new MySqlCommand(sql, db.Conn);
            MySqlDataReader reader = cmd.ExecuteReader();

            Read2List(reader, list);

            if (reader != null)
                reader.Close();

            db.CloseConnection();

            return list.Count==1?true:false;
        }

        public User ValidateUser(string name, string passwd)
        {
            DBConnect db = new DBConnect();
            db.OpenConnection();

            List<User> list = new List<User>();
            string sql = string.Format("select * from user where Name='{0}' and Passwd='{1}'", name, passwd);
            MySqlCommand cmd = new MySqlCommand(sql, db.Conn);
            MySqlDataReader reader = cmd.ExecuteReader();

            Read2List(reader, list);

            if (reader != null)
                reader.Close();

            db.CloseConnection();

            if (list.Count==1)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        public String GetUsernameById(int id)
        {
            DBConnect db = new DBConnect();
            db.OpenConnection();

            List<User> list = new List<User>();
            string sql = string.Format("select * from user where user.id={0}", id);
            MySqlCommand cmd = new MySqlCommand(sql, db.Conn);
            MySqlDataReader reader = cmd.ExecuteReader();

            Read2List(reader, list);

            if (reader != null)
                reader.Close();

            db.CloseConnection();

            if (list.Count == 1)
            {
                return list[0].Name;
            }
            else
            {
                return null;
            }
        }

        public String GetUsernameByItemId(int id)
        {
            DBConnect db = new DBConnect();
            db.OpenConnection();

            List<User> list = new List<User>();
            string sql = string.Format("select * from user,item where item.AuthorId=user.Id and item.Id = {0}", id);
            MySqlCommand cmd = new MySqlCommand(sql, db.Conn);
            MySqlDataReader reader = cmd.ExecuteReader();

            Read2List(reader, list);

            if (reader != null)
                reader.Close();

            db.CloseConnection();

            if (list.Count == 1)
            {
                return list[0].Name;
            }
            else
            {
                return null;
            }
        }
    }
}